﻿using Microsoft.AspNetCore.Mvc;
using MVC系统mvc.Dto.AllocateDto;
using MVC系统mvc.Dto.ManufactureDto;
using Newtonsoft.Json;
using WMS_MAV;
using MVC系统mvc.Dto.Manufacture.WorkOrder;
using MVC系统mvc.Dto.Process;
using MVC系统mvc.Dto.Quality;

namespace MVC系统mvc.Controllers
{
    /// <summary>
    /// 生产管理
    /// </summary>
    public class ManufactureController : Controller
    {
        private readonly IConfiguration _configuration;

        public ManufactureController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
       
        /// <summary>
        /// 获取Token
        /// </summary>
        /// <returns></returns>
        private string ExtractTokenFromHeader()
        {
            if (Request.Headers.TryGetValue("Authorization", out var tokenHeaderValues) && tokenHeaderValues.Count > 0)
            {
                string token = tokenHeaderValues.FirstOrDefault()?.TrimStart("Bearer ".ToCharArray());
                return !string.IsNullOrEmpty(token) ? token : null;
            }
            return null;
        }

        /// <summary>
        /// 生产订单列表
        /// </summary>
        /// <returns></returns>
        public IActionResult OrderTable()
        {
            return View();
        }

        /// <summary>
        /// 生产订单添加
        /// </summary>
        /// <returns></returns>
        public IActionResult OrderAdd()
        {
            return View();
        }

        /// <summary>
        /// 生产订单编辑
        /// </summary>
        /// <returns></returns>
        public IActionResult OrderEdit()
        {
            return View();
        }

        /// <summary>
        /// 生产订单查看
        /// </summary>
        /// <returns></returns>
        public IActionResult OrderShow()
        {
            return View();
        }
        /// <summary>
        /// 添加工单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddWorkOrder()
        {
            return View();
        }
        /// <summary>
        /// 显示工单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowWorkOrder()
        {
            return View();
        }
        /// <summary>
        /// 反填工单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult GetByShowWorkOrder()
        {
            return View();
        }
        /// <summary>
        /// 创建生产计划--视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateProductionPlan()
        {
            return View();
        }

        /// <summary>
        /// 添加工单信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> WorkOrderAdd(AddWorkOrderDto field, string token)
        {
            //http://localhost:45234/
            HttpClientWrapper client = new HttpClientWrapper(token);
            //string url = "http://localhost:45234/api/WorkOrders/CreateWorkorder";
            string url = _configuration["Url"] + "mapi/api/WorkOrders/CreateWorkorder";
            string list = JsonConvert.SerializeObject(field);
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 工单显示
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> WorkOrderList(ShowWorkOrderDto model, string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            //var url = "http://localhost:45234/api/WorkOrders/GetWEorkorderList?WorkOrderCode=" + model.WorkOrderCode + "&WorkOrderSart=" + model.WorkOrderSart + "&CreatedDate=" + model.CreatedDate + "&PageIndex=" + model.PageIndex + "&PageSize=" + model.PageSize;
            var url = _configuration["Url"] + "mapi/api/WorkOrders/GetWEorkorderList?WorkOrderCode=" + model.WorkOrderCode + "&WorkOrderSart=" + model.WorkOrderSart + "&CreatedDate=" + model.CreatedDate + "&PageIndex=" + model.PageIndex + "&PageSize=" + model.PageSize;
            var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 工单信息反填
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> FabPurch(int WorkOrderId, string token)
        {


            HttpClientWrapper client = new HttpClientWrapper(token);
            //string url = "http://localhost:45234/api/WorkOrders/GetIdWorkorder?WorkOrderId=" + WorkOrderId;
            string url = _configuration["Url"] + "mapi/api/WorkOrders/GetIdWorkorder?WorkOrderId=" + WorkOrderId;
            var response = await client.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 工单下发
        /// </summary>
        /// <param name="models"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> XiaFa(XiaFaDto field, string token)
        {

            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            // string url = "http://localhost:45234/api/WorkOrders/XiaFaWorkorder?WorkOrderId=" + field.WorkOrderId + "&WorkOrderSart=" + field.WorkOrderSart + "&SingleNumber=" + field.SingleNumber;
            string url = _configuration["Url"] + "mapi/api/WorkOrders/XiaFaWorkorder?WorkOrderId=" + field.WorkOrderId + "&WorkOrderSart=" + field.WorkOrderSart + "&SingleNumber=" + field.SingleNumber;
            string list = JsonConvert.SerializeObject(field);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        ///暂停/关单
        /// </summary>
        /// <param name="models"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> Guandan(GuandanDto model, string token)
        {

            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            //string url = "http://localhost:45234/api/WorkOrders/ZanGuandanWorkorder?WorkOrderId=" + model.WorkOrderId + "&WorkOrderSart=" + model.WorkOrderSart;
            string url = _configuration["Url"] + "mapi/api/WorkOrders/ZanGuandanWorkorder?WorkOrderId=" + model.WorkOrderId + "&WorkOrderSart=" + model.WorkOrderSart;
            string list = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }

    }
}
